﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Xml;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class LoginForm : Form
    {
        private int randomkey;
        
        public LoginForm()
        {
            InitializeComponent();
            this.skinEngine1.SkinFile = "MP10.ssk";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(!checknoblank())
            {
                return;
            }

            if (!checklength())
            {
                return;
            }
            
            if (!checkrandomkey())
            {
                generaterandomkey();
                return;
            }
            
            if (validateuser())
            {
                MonitorForm mf = new MonitorForm(this);
                mf.Show();
                
                UserLog.addloginrecord(User.UserID, User.Username, User.Userrole, "登陆", User.IPAddress);

                this.Visible = false;
            }
            else
            {
                generaterandomkey();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private bool checknoblank()
        {
            if(this.textBox1.Text == "")
            {
                MessageBox.Show("请输入用户号码");
                return false;
            }
            if(this.textBox2.Text == "")
            {
                MessageBox.Show("请输入密码");
                return false;
            }
            if(this.textBox3.Text == "")
            {
                MessageBox.Show("请输入验证码");
                return false;
            }
            if (this.textBox3.Text != this.label5.Text)
            {
                MessageBox.Show("请输入正确的验证码");
                return false;
            }
            return true;
        }

        private bool checklength()
        {
            if (this.textBox1.Text.Length > 50)
            {
                MessageBox.Show("用户号码不能超过50个字符");
                return false;
            }

            if (this.textBox2.Text.Length > 50)
            {
                MessageBox.Show("用户密码不能超过50个字符");
                return false;
            }
            return true;
        }


           

        private string getipaddress()
        {
            IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;

            if (addressList[0].AddressFamily.ToString() == ProtocolFamily.InterNetwork.ToString())
            {
                return addressList[0].ToString();
            }
            else
            {
                IPAddress ip = IPAddress.IPv6Any;
                return ip.ToString();
            }
        }

        
        private bool validateuser()
        {
            try
            {
                string userid = this.textBox1.Text;
                string password = this.textBox2.Text;
                MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();

                var query = (from c in dbcontext.tb_user
                             where c.ID == userid && c.Password == password
                             select new { c.ID, c.Name, c.Role, c.Password });

                if (query.Count() == 0)
                {
                    MessageBox.Show("没用该用户号码或密码错误");

                    return false;
                }

                User.UserID = query.First().ID;
                User.Username = query.First().Name;
                User.Userrole = query.First().Role;
                User.IPAddress = this.getipaddress();
                User.Password = query.First().Password;

                if (User.Password == "888888")
                {
                    MessageBox.Show("用户密码为初始密码，请尽快修改密码");
                }

                var query1 = from c in dbcontext.tb_userauth where c.userID == User.UserID select c;

                if(query1.Count() != 0)
                {
                     User.auth[0] = query1.First().a;
                     User.auth[1] = query1.First().b;
                     User.auth[2] = query1.First().c;
                     User.auth[3] = query1.First().d;
                     User.auth[4] = query1.First().e;
                }

                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }

        private void generaterandomkey()
        {
            Random ran = new Random();
            randomkey = ran.Next(1000, 9999);
            this.label5.Text = randomkey.ToString();
        }

        private bool checkrandomkey()
        {
            if (this.textBox3.Text == "")
            {
                MessageBox.Show("请输入验证码");
                return false;
            }
            return true;
            
           int key = Int32.Parse(this.textBox3.Text);
            if(key == randomkey)
            {
                return true;
            }
            else
            {
                MessageBox.Show("请输入正确的验证码");
                return false;
            }
        }
        

        private void label5_Click(object sender, EventArgs e)
        {
            generaterandomkey();  
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            generaterandomkey();
            this.textBox3.Text = null;
            XmlDocument doc = new XmlDocument();

            if (File.Exists("Monitor.exe.config"))
            {
                doc.Load("Monitor.exe.config");
            }
            else
            {
                MessageBox.Show("找不到配置文件,请重新配置数据库连接");
                return;
            }

            XmlNodeList nodeList = doc.SelectNodes("/configuration/connectionStrings/add");

            UserLog.connectionstring = nodeList[0].Attributes[1].Value;
            Console.WriteLine(UserLog.connectionstring);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            DataConfigForm f = new DataConfigForm();
            f.Show();
        }

        private void button1_MouseEnter(object sender, EventArgs e)
        {
            button1.BackColor = Color.LightBlue;
        }

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            button1.BackColor = Color.Orange;
        }

        private void button1_MouseLeave(object sender, EventArgs e)
        {
            button1.BackColor = Color.White;
        }

        private void button2_MouseEnter(object sender, EventArgs e)
        {
            //button2.BackColor = Color.LightBlue;
        }

        private void button2_MouseLeave(object sender, EventArgs e)
        {
            //button2.BackColor = Color.White;
        }

        private void button2_MouseDown(object sender, MouseEventArgs e)
        {
           // button2.BackColor = Color.Orange;
        }

        private void button3_MouseEnter(object sender, EventArgs e)
        {
            button3.BackColor = Color.LightBlue;
        }

        private void button3_MouseLeave(object sender, EventArgs e)
        {
            button3.BackColor = Color.White;
        }

        private void button3_MouseDown(object sender, MouseEventArgs e)
        {
            button3.BackColor = Color.Orange;
        }
      
    }
}